﻿#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QTcpSocket>
#include <QObject>


class TcpClient : public QObject {
    Q_OBJECT
  private:
    TcpClient();
  public:

    ~TcpClient();

    bool initConnect();

    static TcpClient *getClient() {
        static TcpClient tcpClient;
        return &tcpClient;
    }

    QTcpSocket *getSocket();

    // 原始数据做base64转码发送
    bool sendData(QString &data);

    // 读取数据需要做同样操作
    bool readData(QString &readBuf);

    // 连接判断
    bool isConnected();

    // 关闭连接
    void disconnectServer();

  signals:
    void connectChangeSignal(bool isConnect);

  private:
#define HOST "139.224.247.174"
#define PORT 8000
    QTcpSocket *tcpSocket;
    bool isConnect;    // true:服务器已连接,false:服务器未连接

  private slots:
    // 连接状态变化的时候触发
    void slotStateChanged(QAbstractSocket::SocketState state);
};

#endif // TCPCLIENT_H
